package me.godwin.datastructures;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;


/**
 * Dictionary of words stored in a Trie.
 *  cd sr   
 * @author Godwin Babu
 *
 */
public class Dictionary {
    private static final String FILE = "/usr/share/dict/words";
    private final String fileName;
    private final Trie trie;

    public Dictionary() throws IOException {
        this(null);
    }

    public Dictionary(final String fileName) throws IOException {
        if (fileName == null) {
            this.fileName = FILE;
        } else {
            this.fileName = fileName;
        }
        BufferedReader in = new BufferedReader(new FileReader(this.fileName));
        trie = new Trie();
        String line;
        int c = 0;
        while((line = in.readLine()) != null) {
            trie.addWord(line);
            c++;
        }
        return;
    }
    
    public boolean checkWord(final String word) { return trie.checkWord(word); }
    public List<String> listSuffix(final String prefix) throws Trie.TooManyMatchesException { return trie.listSuffix(prefix); }
    public List<String> listWords() throws Trie.TooManyMatchesException { return trie.listWords(); }
    public void printWords() { trie.printWords(); }
    public void printWords1() { trie.printWords1(); }    
    
    public static void main(final String[] args) {
        Runtime runtime = Runtime.getRuntime();
        long start = 0, end = 0;
        Dictionary dict;
        runtime.gc();
        start = runtime.freeMemory();
        try {
            dict = new Dictionary();
            dict.trie.printWords1();
        } catch(Exception e) {
            e.printStackTrace();
        }
        end = runtime.freeMemory();
        System.out.println("Size of Dictionary: " + (start - end) + " start: " + start + " end: " + end);
//        try {
//            Thread.sleep(50000L);
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    }
}
